/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid.util;

import com.blandware.atleap.common.util.StringUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>Filter for sets. This filter adds possibility to select specified value from fixed set.
 * This set is obtained from persistence store. <br />
 * This field can contain only one condition.
 * </p>
 * <p><a href="SetFilter.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.11 $ $Date: 2007/12/08 17:51:55 $
 */
public class SetFilter extends BaseFilter {

	// ~ Static variables
	public static final String AVAILABLE_ELEMENTS = "com.blandware.atleap.taglib.grid.util.SetFilter.AVAILABLE_ELEMENTS";

	// ~ Instance variables

	/**
	 * List contains elements to display for choosing those which must be included in filter
	 */
	private Map availableElements = Collections.synchronizedMap(new HashMap());

	/**
	 * List contains indices of selected elements
	 */
	private Map selectedElements = Collections.synchronizedMap(new HashMap());

	private String firstCondition = FilterConditions.FIELD_IN_SET;

	private String secondCondition = FilterConditions.CONDITION_NOT_SET;
	private String logicCopula = FilterConditions.LOGIC_COPULA_AND;


	/**
	 * Creates new instance of SetFilter
	 *
	 * @param fieldName Name of field to associate filter with
	 */
	public SetFilter(String fieldName) {
		super(fieldName);
		firstCondition = FilterConditions.FIELD_IN_SET;
	}

	/**
	 * Creates clause for set filter
	 *
	 * @param condition Condition to put in clause
	 * @return Clause
	 */
	private String createClause(String condition) {
		if ( condition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || condition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
			return createNullComparison(condition);
		}
		String setString = "(";
		for ( Iterator i = selectedElements.keySet().iterator(); i.hasNext(); ) {
			String key = (String) i.next();
			setString += "\'" + StringUtil.sqlEscape(key) + "\'";
			if ( i.hasNext() ) {
				setString += ", ";
			} else {
				setString += ") ";
			}
		}

		return fieldName + " " + condition + " " + setString;
	}

	/**
	 * Creates first clause
	 *
	 * @param condition Congruence condition
	 */
	public void createFirstClause(String condition) {
		firstCondition = condition;
		setFirstClause(createClause(condition));
	}

	/**
	 * This filter's second condition can only be the null comparison, otherwise it will do nothing
	 *
	 * @param condition     Congruence condition
	 * @param logicCopula   Logic copula to use
	 */
	public void createSecondClause(String condition, String logicCopula) {
		this.secondCondition = condition;
		this.logicCopula = logicCopula;
		if ( condition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || condition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
			setSecondClause(createNullComparison(condition), logicCopula);
		}
	}

    /**
     * Returns list containing elements to display for choosing those which must
     * be included in filter
     *
     * @return list of available elements
     */
	public Map getAvailableElements() {
		return availableElements;
	}

    /**
     * Sets list containing elements to display for choosing those which must
     * be included in filter
     *
     * @param availableElements list of available elements to set
     */
	public void setAvailableElements(Map availableElements) {
		this.availableElements = availableElements;
	}

    /**
     * Returns list containing indices of selected elements
     *
     * @return list of selected elements indices
     */
	public Map getSelectedElements() {
		return selectedElements;
	}

    /**
     * Sets list containing indices of selected elements
     *
     * @param selectedElements list of selected elements indices to set
     */
	public void setSelectedElements(Map selectedElements) {
		this.selectedElements = selectedElements;
	}

    /**
     * Returns first condition
     *
     * @return first condition
     */
	public String getFirstCondition() {
		return firstCondition;
	}


}
